<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.7 Object Serialization</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 15:51:12 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>What's New in Java 1.1</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-4-SECT-7">4.7 Object Serialization</A></h2>

<P CLASS=para>
Object serialization is one of the major new features of Java 1.1.
It refers to the ability to write the complete state of an
object (including any objects it refers to) to an output
stream, and then recreate that object at some later time by
reading its serialized state from an input stream.  You can
serialize an object simply by passing it to the
<tt CLASS=literal>writeObject()</tt> method of an
<tt CLASS=literal>ObjectOutputStream</tt>.  Similarly, you can create an object
from a serialized object stream by calling the
<tt CLASS=literal>readObject()</tt> method of an <tt CLASS=literal>ObjectInputStream</tt>.
Both of these new object stream types are part of the
<tt CLASS=literal>java.io</tt> package.

<P CLASS=para>
Typically, object serialization is as simple as calling
<tt CLASS=literal>writeObject()</tt> and <tt CLASS=literal>readObject()</tt>.  There are a
few additional twists, however, that are worth mentioning
here.  First, only objects that subclass the
<tt CLASS=literal>Serializable</tt> (or <tt CLASS=literal>Externalizable</tt>)
interface can be serialized.  The <tt CLASS=literal>Serializable</tt>
interface does not define
any methods, but merely acts as a marker that indicates whether
serialization is allowed on a given object.  Second, fields
of a class declared <tt CLASS=literal>transient</tt> are not serialized as
part of an object's state.  The <tt CLASS=literal>transient</tt> modifier
was legal in Java 1.0, but had no defined behavior.  Third, some
objects may need to implement custom serialization or
de-serialization behavior.  They can do this by implementing
special <tt CLASS=literal>readObject()</tt> and <tt CLASS=literal>writeObject()</tt>
methods. <A HREF="ch09_01.htm">Chapter 9, <i>Object Serialization</i></A> describes all
of these aspects of object serialization in more detail.

<P CLASS=para>
Despite the fact that only a few classes and interfaces are
part of the Object Serialization API, serialization is a very important
technology and is used in several places in Java 1.1.  It
is used as the basis for transferring objects via
cut-and-paste.  It is used to transfer objects between
a client and a server for remote method invocation.
It is used by the JavaBeans API--beans are
often provided as pre-initialized, serialized objects, rather than
merely as class files.  Java 1.1 also adds the capability
for applets to be loaded into an applet viewer or browser as
serialized objects.  One common use we are likely to see for
object serialization is as a way to save user preferences
and other application states--a serialized object is an
instant file format that works for any application.  Another
use that should be popular with GUI builder tools is
saving the complete <tt CLASS=literal>Component</tt> hierarchy of an
application's GUI as a serialized object, and then later loading
in that object in order to automatically recreate the GUI.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Internationalization</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Reflection</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
